package com.zpphis.controller;

import com.zpphis.entity.Menu;
import com.zpphis.entity.UserMenu;
import com.zpphis.service.IUserMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user_menu")
public class UserMenuController {

    @Autowired
    private IUserMenuService userMenuService;

    @RequestMapping("/table.json")
    public Map<String, Object> table() {
        return new HashMap<String, Object>() {
            {
                put("code", 0);
                put("data", userMenuService.listExtend());
            }
        };
    }

    @RequestMapping("/add.json")
    public Map<String, Object> add(int userType, int menuId) {
        UserMenu userMenu = new UserMenu();
        userMenu.setUserTypeId(userType);
        userMenu.setMenuId(menuId);
        if (userMenuService.save(userMenu)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "新增失败！");
                }
            };
        }
    }

    @RequestMapping("/del.json")
    public Map<String, Object> del(int id) {
        if (userMenuService.removeById(id)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "删除失败！");
                }
            };
        }
    }

    @RequestMapping("/queryOne.json")
    public UserMenu queryOne(int id) {
        return userMenuService.getById(id);
    }

    @RequestMapping("/edit.json")
    public Map<String, Object> edit(int userType, UserMenu userMenu) {
        userMenu.setUserTypeId(userType);
        if (userMenuService.updateById(userMenu)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "修改失败！");
                }
            };
        }
    }

}
